var drection = 0; //箭头方向 0 为向下
var margin_top = 30; //
function slide(){
	var speed = 10;
	if(drection == 0)
	   margin_top++;
	else
	   margin_top--;
    if(margin_top > 60){
	       drection = 1;
		}
		if(margin_top < 30 ){
			drection = 0;
			speed = 500;
		}

	$("#intro .blink").css("margin-top",margin_top);

	setTimeout(function(){
		slide();
	},speed);
}
slide();
$(document).ready(function (){


	var script_url = $('#script_url').val();
	flowplayer("flash_player", script_url + "/flowplayer/flowplayer-3.2.16.swf");

	$('.playbtn').click(function(){
		$('#flash_player').show();
	});


	// function slide(){
	// 	$(".blink").animate({"margin-top":30},800)
	// 	.animate({"margin-top":60},800,slide);
	// }
	// slide();
	//点击动态箭头  页面进行滚动
	$(".blink_wrap").click(function(){

		var i = $(this).attr("index");
		var fe = document.getElementById("fe"+i);
		var scrollflag = (fe != null) ? fe.offsetTop -62 : 0;

		$("html,body").animate({scrollTop : scrollflag},800);

	});

	var flag = {
		'username' : 0,
		'password' : 0
	};	

	// 验证密码 返回一个result对象  result.message=null 时 表示验证通过 
	function checkValidPasswd(str){
		var result = {},
		reg = /^[A-Za-z0-9]+$/;
        if (!reg.test(str))
         result.message = "密码只能使用英文和数字";
        else if (str.length < 6 || str.length > 24)
         result.message = "密码需长度6-24";

        return result;
    }

	function checkEmail(str){
		var result={},
			reg=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	    if(!reg.test(str)){
	    	result.message="邮箱格式不正确";
	    }else{
		   	var checkUrl = $('#emailCheckUrl').val() + '?email=' + str;
       		$.ajax({
       			  type:"GET",
    			  url:checkUrl,
    			  async:false,
    			  dataType:"json",
 				  success:function(data){
 				  	switch(data.returnCode){
						case 'exist' :
							result.message = '该用户名已存在';
						break;

						case 'pattern_not_right' :
							result.message = '邮箱格式不正确';
						break;

						case 'success' :;
						break;
					}
 				  }
       		});
	    }
	    return result;
	}
	function checkHandler(e){
		var nameType = $(e).attr("name"),
	    isEmpty = ($(e).val() == ""),
	    result = {};

        if(isEmpty){
              flag[nameType] = 0;
        }else{
        	if(nameType === 'password'){
        		result=checkValidPasswd(e.value);
        	}else if(nameType === 'username'){
        		result=checkEmail(e.value);
        	}
        }
        
	    var $noticewrap = $(e).parent().children(".noticewrap");

	    if(result.message != null){//result.message 不为null时 显示错误
	    	$noticewrap.fadeIn("500").children(".notice").html(result.message);
	    	flag[nameType] = 0;
	    }else{
	    	$noticewrap.fadeOut("600");
	    	flag[nameType] = 1;
		}
	}

	var showBox = function(boxname){
		// $(".overlay").css({
		// 	'width':$(window).width(),
		// 	'height':$(document).height(),
		// 	'display':'block'
		// });
		$('.overlay').show();

		$(boxname).css({
		    'display':'block',
			'left':($(window).width()-492)/2,
			'top': $(window).scrollTop() + 155
		});
		$(boxname).find('.box_inputtext').placeholder();
	};

	//登陆框弹层
	$(".nav li").click(function(){
		var boxname = "."+$(this).attr("box-name");
		showBox(boxname);
	});

	$('#intro .submit').click(function(){
		showBox('.signupbox');
	});

	//关闭登陆框弹层
	$(".box_close").click(function(){
		$(this).parent()
			.css("display","none")
		$(".overlay")
			.css("display","none");
	});

	$('.loginform').submit(function(){
	
		var $this = $(this),
			username = $this.find('[name="username"]').val(),
			password = $this.find('[name="password"]').val();

		if(!username || !password){
			$this.find('.box_notice').text('请输入邮箱和密码');
			return false;
		}

		$.ajax({
			url : $(this).attr('action'),
			dataType : 'json',
			type : 'POST',
			data : $(this).serialize(),
			success : function(data){
				if (typeof data.exception === 'undefined' && data.redirectUrl) {
					location.href = data.redirectUrl;
				}else{
					$this.find('.box_notice').text('邮箱或密码错误');
				}
			}
		});
		return false;
	});

	$(".showpwdwrap").click(function(){
		var $section= $(this).parent();
		var $input = $('#password_input',$section),
			$radio = $(this).find('.radio');

		var newInput = document.createElement('input');

		if( $radio.hasClass("select") ){
			$radio.removeClass("select");
			newInput.type = 'password';
		}else{
			$radio.addClass("select");
			newInput.type = 'text';
		}

		newInput.id = 'password_input';
		newInput.className = 'text_input box_inputtext password_input';
		newInput.name = 'password';
		newInput.placeholder = '设置一个密码';
		newInput.maxLength = '24';

		newInput.value = $input.val();

		$input.replaceWith($(newInput));
	});

	$(".signup_form").on("blur","input",function(){
		var nameType = $(this).attr("name"),
	    isEmpty = ($(this).val() == ""),
	    result = {};

        if(isEmpty){
              flag[nameType] = 0;
        }else{
        	if(nameType === 'password'){
        		result=checkValidPasswd(this.value);
        	}else if(nameType === 'username'){
        		result=checkEmail(this.value);
        	}
        }
        
	    var $noticewrap = $(this).parent().children(".noticewrap");

	    if(result.message != null){//result.message 不为null时 显示错误
	    	$noticewrap.fadeIn("500").children(".notice").html(result.message);
	    	flag[nameType] = 0;
	    }else{
	    	$noticewrap.fadeOut("600");
	    	flag[nameType] = 1;
		}
	});
	$('.signup_form').submit(function(){

		var $form  = $(this);
		var usernameValue = $('[name="username"]',$form).val(),
			passwordValue = $('[name="password"]',$form).val();

		if(!usernameValue || !passwordValue){
			return false;
		}

		if(!flag.username || !flag.password){
			return false;
		}	

	});

	// 视频播放相关
	var script_url = $('#script_url').val();

	$('.playbtn').click(function(){
		flowplayer("flash_player", script_url + "/flowplayer/flowplayer-3.2.16.swf");
		var videoImgOffset = $('.video').offset();
		$('#video_player').css({left:videoImgOffset.left,top:videoImgOffset.top});

		var left = ( $(window).width() - 960 ) / 2,
			top  =  $(window).scrollTop() + 100;
		$('.overlay').show();
		$('#video_player').show().animate({width: '960px', height : '540px', left : left, top : top},500);

		return false;
	});

	$('#video_player .close_btn').click(function(){
		var videoImgOffset = $('.video').offset();
		$('.overlay').hide();
		$('#video_player').animate({width: '527px', height : '335px',left : videoImgOffset.left,top:videoImgOffset.top },500,function(){
			$('#flash_player').html('');
			$('#video_player').hide();
		});
	});

	$('.signup .text_input').placeholder();
	// $('input').placeholder();

});